package com.Jian.reggie.service.impl;

import com.Jian.reggie.common.BaseContext;
import com.Jian.reggie.common.CustomException;
import com.Jian.reggie.impl.*;
import com.Jian.reggie.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.Jian.reggie.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author 12646
 * @description 针对表【orders(订单表)】的数据库操作Service实现
 * @createDate 2023-11-29 14:55:41
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService {
  @Autowired
  private ShoppingCartService shoppingCartService;
  @Autowired
  private UserService userService;
  @Autowired
  private AddressBookService addressBookService;
  @Autowired
  private OrderDetailService orderDetailService;
  
  @Override
  public void submitOrder(Orders orders) {
    Long currentId = BaseContext.getCurrentId();
    // 查询当前用户购物车数据
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ShoppingCart::getUserId, currentId);
    List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);
    // 查询用户数据和地址数据
    User user = userService.getById(currentId);
    AddressBook address = addressBookService.getById(orders.getAddressBookId());
    if (address == null) {
      throw new CustomException("用户地址信息有误，不能下单");
    }
    
    // 向订单表插入数据，一条
    long id = IdWorker.getId();// 订单号
    AtomicInteger amount = new AtomicInteger(0);
    // 遍历购物车数据获取amount
    List<OrderDetail> orderDetails = shoppingCarts.stream().map((item) -> {
      OrderDetail orderDetail = new OrderDetail();
      orderDetail.setOrderId(id);
      orderDetail.setNumber(item.getNumber());
      orderDetail.setDishFlavor(item.getDishFlavor());
      orderDetail.setDishId(item.getDishId());
      orderDetail.setSetmealId(item.getSetmealId());
      orderDetail.setName(item.getName());
      orderDetail.setImage(item.getImage());
      orderDetail.setAmount(item.getAmount());
      amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
      return orderDetail;
    }).collect(Collectors.toList());
    
    
    orders.setId(id);
    orders.setOrderTime(LocalDateTime.now());
    orders.setCheckoutTime(LocalDateTime.now());
    orders.setStatus(2);
    orders.setAmount(new BigDecimal(amount.get()));
    orders.setUserId(user.getId());
    orders.setNumber(String.valueOf(id));
    orders.setUserName(user.getName());
    orders.setConsignee(address.getConsignee());
    orders.setPhone(address.getPhone());
    orders.setAddress((address.getProvinceName() == null ? "" : address.getProvinceName())
        + (address.getCityName() == null ? "" : address.getCityName())
        + (address.getDistrictName() == null ? "" : address.getDistrictName())
        + (address.getDetail() == null ? "" : address.getDetail()));
    
    this.save(orders);
    
    // 向订单详情表插入数据，多条
    orderDetailService.saveBatch(orderDetails);
    
    // 从购物车表删除
    shoppingCartService.remove(wrapper);
  }
}




